﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Slides;
using VK.Comm.TransactSql;
using VK.Casemeet.Photos;
using VK.Casemeet.Employees;
using System.Web.Script.Serialization;
using System.Data;

namespace VK.Casemeet.Web.Admin.Slides
{
    public partial class SlideEdit : System.Web.UI.Page
    {
        public string imgUrl = "Null.jpg";

        public int slideID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="焦点图列表",Url="/Admin/Slides/SlideList.aspx"},
                new MapPath{ Text="编辑",Url="#"}
            };

            slideID = Util.GetQueryInt("SlideID");

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (slideID != 0)
            {
                SlideEntity slide = SlideBLL.GetSlide(slideID);
                lbExplain.Text = slide.Explain;
                lbWidth.Text = slide.Width.ToString();
                lbHeight.Text = slide.Height.ToString();

                IList<Condition> conditionPhotolst = new List<Condition>();
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectType", ConditionType = ConditionType.NumeralEqual, ConditionValue = 4, DbType = DbType.Int32 });
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectID", ConditionType = ConditionType.NumeralEqual, ConditionValue = slideID, DbType = DbType.Int32 });
                rptPhoto.DataSource = PhotoBLL.GetPhotoList(conditionPhotolst, null, SortMode.Asc);
                rptPhoto.DataBind();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            SlideEntity slide = LoadEntity();
            slide.SlideID = slideID;

            if (SlideBLL.UpdateSlide(slide) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected SlideEntity LoadEntity()
        {
            SlideEntity slide = SlideBLL.GetSlide(slideID);
            PhotoEntity[] imgUrlArray = new JavaScriptSerializer().Deserialize<PhotoEntity[]>(Util.GetFormString("hidImgUrlArray"));
            slide.ImgUrllst = new List<PhotoEntity>(imgUrlArray);

            return slide;
        }
    }
}